package com.leetcode.bfs;

public class Leetcode110 {
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }

        if (Math.abs(bfs(root.left) - bfs(root.right)) > 1) {
            return false;
        }

        return isBalanced(root.left) && isBalanced(root.right);
    }

    // 计算子树的高度
    private int bfs(TreeNode root) {
        if (root == null) {
            return 0;
        }

        return Math.max(bfs(root.left), bfs(root.right)) + 1;
    }
}
